package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.ClientDao;
import com.dc.model.Client;
import com.dc.service.ClientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
//    对类成员变量、方法及构造函数进行标注,完成自动装配的工作,消除get set
    @Autowired
    ClientDao clientDao;
    // 分页查询客户信息(带搜索)
    public BaseModel selectPageClient(Integer curr_page, Integer page_size, BaseModel baseModel, String address, String client_name,Integer type) {
            // 编写业务，实现分页 limit   (mybatis分页工具)
            PageHelper.startPage(curr_page,page_size);
            // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
            List<Client> clients=clientDao.selectPageClient(address,client_name,type);
            // 在new PageInfo<>(list)的时候可以把页码、页大小、总页数等信息给pageinfo，navigate导航
            PageInfo pageInfo=new PageInfo(clients,5);
            // 设置结果code
            baseModel.setResultCode(0);
            // 设置消息提示
            baseModel.setMessage("分页查询客户成功！");
            // 插入数据
            baseModel.setData(pageInfo);
            // 返回basemodel
            return baseModel;
    }

    // 新增客户的接口
    public BaseModel addClient(Client client, BaseModel baseModel) throws Exception {
        // 判断是否本客户已经添加
        Integer clientS = clientDao.selectClientByName(client.getClient_name());
        if(clientS>0){
            // 有账号不准插入
            throw new BusinessException(ErrorMessageEnum.REPEAT_CLIENT);
        }
        //调用新增客户的dao
        Integer result=clientDao.insertClient(client);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增客户成功！");
        return baseModel;
    }

    // 修改客户的接口
    public BaseModel updateClient(Client client, BaseModel baseModel) throws Exception {
        Integer result=clientDao.updateClient(client);
        if(result<=0){
            throw  new BusinessException(1008,"修改客户失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改客户成功！");
        return baseModel;
    }

    // 删除客户的接口
    public BaseModel deleteClient(String ids, BaseModel baseModel) throws Exception {
        String[] idArry=ids.split(",");
        int result=clientDao.deleteClient(idArry);
        if(result<=0){
            throw new BusinessException(1009,"删除"+result+"个客户失败。");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"个客户成功！");
        return baseModel;
    }

    // 根据id查询客户信息的接口
    public BaseModel selectClientById(Integer client_id,BaseModel baseModel) throws Exception{
        List<Client> clientList = clientDao.selectClientById(client_id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询单个客户成功");
        baseModel.setData(clientList);
        return baseModel;
    }
}
